<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<script src="./jquery-3.6.0.js"></script>

<body>
  <input type="checkbox" name="aa">苹果
  <input type="checkbox" name="aa">橘子
  <input type="checkbox" name="aa">荔枝
  <input type="checkbox" name="aa">葡萄
  <input type="checkbox" id="all">全选<br>
  <button>反选</button>
</body>
<script>
  $(function () {
    console.log($("#all").checked);
    $("#all").change(function () {
      $("[name='aa']").prop("checked", $(this).prop("checked"))
    })
    
    $("[name='aa']").change(function () {
      var chex = $("[name='aa']")
      console.log(chex);
      var flag=true
      for (var i = 0; i < chex.length; i++) {

        if (!chex[i].checked) {
        flag=false
        }
      }
      $("#all").prop("checked",flag)
    })
    //反选
    $("button").click(function(){
      var chex=$("[name='aa']")
      for(var i = 0; i<chex.length;i++){
        $(chex[i]).prop("checked",!$(chex[i]).prop("checked"))
      }
    })
  })

</script>

</html>